<#include "custom.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
$(document).ready(function() {

	var send = {};
	
	$.extend(send, {
		data : {
			submit : false
		},
		init : {
			initData : function() {
				var obj = getCache('${classNameLower}_add_id');
			
				send.init.initEvent(obj);
				
				if (obj != null) {
					send.data.obj = obj;
					$('title').html('企脉-编辑产品');
					$('#pageTitle').html('编辑产品');
					send.func.initData(obj);
				}else{
					$('title').html('企脉-新建产品');
					$('#pageTitle').html('新建产品');
				}
			},
			initEvent : function(obj) {
				$('#btnFile').on('touchend click', send.func.selectFile);
				$("#btnUpload").on('touchend click', send.func.fileUpload);

				if($.isEmptyObject(obj)){
					$('#btnSend').on('touchend click', send.func.doAdd);
				}else{
					$('#btnSend').on('touchend click', send.func.doSave);//编辑操作
				}
				
				$('#btnPreview').on('touchend click', function() {
					$('#${classNameLower}Content').html(UE.getEditor('container').getContent());
				});
				
			}
		},
		func : {
			initData : function(obj){
				if(obj){
					$('#${classNameLower}Name').val(obj.name);
					$('#${classNameLower}Sort').val(obj.sort);
					$("#uploadImage").attr("src", ftpUrl + obj.minPicPath);
					$("#aUploadImage").attr("href", ftpUrl + obj.minPicPath);
					UE.getEditor('container').addListener("ready", function () {
						UE.getEditor('container').setContent(obj.content.replace(/#qile_image_server#/g, ftpUrl));
					});
					$('#fileName').attr('path', obj.minPicPath);
					$('#btnSend').html('保存');
				}
			},
			doPost : function(operate) {
				if (send.data.submit) {
					alert('正在提交数据，请不要重复点击！');
					return false;
				}

				if (!$("#form").valid()) {
					return;
				}

				var params = {};
				
				params.operate = operate;//1：新建，2：编辑
				
				params.name = $('#${classNameLower}Name').val();
				params.sort = $('#${classNameLower}Sort').val();
				
				params.content = getUrlContent();
				if(!params.content){
					alert("请填写产品内容");
					return false;
				}
				
				params.minPicPath  = $('#fileName').attr('path');
				if (!params.minPicPath) {
					alert('请上传封面图片');
					return false;
				}
				
				var pointIndex = params.minPicPath.lastIndexOf(".");
				var _index = params.minPicPath.lastIndexOf("_");
				var pre = params.minPicPath.substring(0, _index);
				var last = params.minPicPath.substring(pointIndex);
				params.maxPicPath = pre + last;
				params.middlePicPath = pre + "_middel" + last;

				send.data.submit = true;
				var url = '../../${classNameLower}/add.do';
				if(operate == 2){
					url = '../../${classNameLower}/edit.do';
					var obj = send.data.obj;
					if($.isEmptyObject(obj)){
						alert("不能获取到产品id，请刷新页面");
					}else{
						params.id = obj.id;
					}
				}
				$.post(url, params, function(data) {
					send.data.submit = false;

					if (data.status == 0) {
						if(operate == 1){
							$('#${classNameLower}Name').val('');
							$("#uploadImage").removeAttr('src');
							$("#aUploadImage").removeAttr('href');
							$('#fileName').val('');
							$("#fileName").attr('path', '');
						}
						popUpWindow({
							title : "操作成功",
							closeTime : "600",
							popType : 2,
							callback : function () {
								jump('../../pages/${classNameLower}/${classNameLower}_list.html');
							}
						});
					}else{
						alert("系统异常");
					}
				}, 'json');
			},
			doAdd : function(){
				send.func.doPost(1);
			},
			doSave: function(){
				send.func.doPost(2);
			},
			selectFile : function() {
				$('#result').empty();
			},
			fileUpload : function() {
				var name = $('#file1').val();
				if (name.length == 0) {
					$('#result').text("请选择一张图片");
					return false;
				}

				var ext = name.substring(name.lastIndexOf('.'));
				if (ext == '.jpg'
						|| ext == '.jpeg'
						|| ext == '.bmp'
						|| ext == '.png'
						|| ext == '.gif') {
					ajaxFileUpload();
				} else {
					$('#result').text("只支持.jpg.jpeg.bmp.png.gif图片格式");
				}
			}
		}
	});
	
	send.init.initData();
	
	function getUrlContent(){
		var msg = UE.getEditor('container').getContent();
		if(msg){
			msg = msg.replace(new RegExp(ftpUrl, 'g') ,'#qile_image_server#');
		}
		return msg;
	}
	

	$('#file1').on('change', fileChange);

	function fileChange(event) {
		var name = $(this).val();
		name = name.substring(name.lastIndexOf('\\') + 1);
		$('#fileName').val(name);
	};
	
	$('#fileName').val('');
	function ajaxFileUpload() {
		$("#loading").show();
		$('#result').text('正在上传...');

		$.ajaxFileUpload({
			//处理文件上传操作的服务器端地址(可以传参数,已亲测可用)
			url : "../../Upload/upload/2.do",
			secureuri : false, //是否启用安全提交,默认为false 
			fileElementId : "file1", //文件选择框的id属性
			dataType : "json", //服务器返回的格式,可以是json或xml等
			success : function(data, status) { //服务器响应成功时的处理函数
				if (data.status == 1)  { //0表示上传成功(后跟上传后的文件路径),1表示失败(后跟失败描述)
					$('#result').text("图片上传成功");
					setTimeout(function() {
						$('#result').text('');
					}, 1500);
					$("#uploadImage").attr("src", ftpUrl + data.data.maxPicPath);
					$("#aUploadImage").attr("href", ftpUrl + data.data.minPicPath);
					$("#fileName").attr('path', data.data.minPicPath);
				} else {
					$('#result').html(data.error);
				}
			},
			error : function(data, status, e) { //服务器响应失败时的处理函数
				$('#result').html('上传失败，请重试！！' + e);
			},
			complete : function() {
				$("#loading").hide();
				$('#file1').replaceWith($('<input type="file" name="file1" id="file1"'
						+ 'style="filter:alpha(opacity=0);opacity:0;position:absolute;height:110%;width:110%;left:0;top:0;"/>'));
				$('#file1').on('change', fileChange);
				$('#fileName').val('');
			}
		});
	}
	
	$("#form").validate({
		rules :	{
			${classNameLower}Name : {required:true,maxlength:30},
			${classNameLower}Sort : {required:true,number:true}
		}
	});
	
});
